import http from '@/utils/axios'
const state = {
  token: uni.getStorageSync("token") || "",
  isLogin: uni.getStorageSync("isLogin") || false, // 是否登陆,
  userInfo: uni.getStorageSync("userInfo") || {}, // 用户信息
  authType: '' // smsLogin:手机号登录注册, accountLogin:密码登录, forgotPwd:忘记密码, changePwd:修改密码, bindMobile:绑定手机号
}

const mutations = {
  token(state, payload) {
  	state.token = payload;
  	uni.setStorageSync("token", payload);
  },
  AUTH_TYPE(state, data) {
  	data ? uni.hideTabBar() : uni.showTabBar();
  	state.authType = data;
  }
}

const actions = {
  // 登录弹窗控制
  showAuthModal({commit}, type = 'accountLogin') {
  	commit('AUTH_TYPE', type);
  },
  // 登录态
  isLogin(state, data) {
  	state.isLogin = data;
  	uni.setStorageSync('isLogin', data);
  },
  // 用户信息
  userInfo(state, data) {
  	state.userInfo = data;
  	uni.setStorageSync("userInfo", data);
  },
  // 退出登录
  logout({
  	commit,
  	dispatch
  }) {
  	uni.getStorageSync('token') && http('user.logout');
  	commit('token', "");
  	commit('isLogin', false);
  	commit('userInfo', {});
  }
}

export default {
	state,
	mutations
}